<template>
  <div class="login_box">
      <div class="login_top_btn">
          <!--<a>忘记密码</a>-->
          <!--<router-link to="/register" style="color:#fff;">注 册</router-link>-->
      </div>

      <div class="login_main">
        <h2>SHPay管理平台</h2>
        <input class="width_full" type="text" placeholder="用户名" v-model="userName" @keyup.13="keyDown($event)" style="background:white;">
        <input class="width_full" type="password" placeholder="密码" v-model="password" @keyup.13="keyLogin($event)" ref="inputPsw">
        <el-row :gutter="20">
            <!--<el-col :span="12" style="line-height: 44px;margin-top:10px;text-align: center">
                <router-link to="/register" style="color:#fff;">注 册</router-link>
            </el-col>-->
            <el-col :span="24">
                <input class="width_full" type="button" value="登 录" @click="login" ref="loginBtn" style="background: #00AAFF;">
            </el-col>
        </el-row>
      </div>
      <div class="login_bottom">
        <!--Copyright © 青岛红柿子软件技术有限公司版权所有 备案号：鲁ICP备14000000-1号-->
      </div>
  </div>
</template>


<script>
var qs = require('qs')

export default {
	// 在components字段中，包含导入的子组件
	name: 'app',
	data() {
		return {
			userName: '',
			password: '',
			logonMode: 1,
		}
	},
	components: {},
	methods: {
		login() {
			this.$axios
				.post(API.oauth, {
					clientId: '098f6bcd4621d373cade4e832627b4f6',
					accountName: this.userName,
					accountPwd: this.password,
					logonMode: this.logonMode,
				})
				.then(
					response => {
                        console.log(response.data)
                        //取到数据做验证
						if (response === null && response === undefined) {
							this.$message.error('网络错误请刷新重试')
						} else if (response.data.status == 0) {
							var token = response.data.data.accessToken
							sessionStorage.setItem('accessToken', token)
							console.log(
								'这是我保存的token++',
								sessionStorage.getItem('accessToken'),
							)
							console.log('token', token)
							sessionStorage.Authorization = token
							localStorage.Authorization = token
							this.setCookie('token', token, 1)
							this.setCookie(
								'userId',
								response.data.data.userId,
								1,
							)
							localStorage.userId = response.data.data.userId
							localStorage.deptId = response.data.data.deptId
							localStorage.userName = response.data.data.realName
							localStorage.avatar = response.data.data.avatar
							localStorage.perms = response.data.data.perms
							console.log(localStorage.avatar)
							//查询Company信息
							// this.$http.get(API.rootPath + "/companies/depts/id/" + localStorage.deptId).then((response) => {
							//     if (response.body.status == 0) {
							//         localStorage.companyId = response.body.data.id;
							//         localStorage.companyName = response.body.data.name;

							//         // console.log("登陆完初始化头部啦", localStorage.userName);

							//     }
							// })

							eventBus.$emit('initLeftMenu')
							eventBus.$emit('initTopBar')
							this.$router.push('/')
						} else {
							this.$message.error(response.data.msg)
						}
					},
					response => {},
				)
		},
		keyLogin(ev) {
			//                if (ev.keyCode == 13)  //回车键的键值为13
			// console.log('你按回车键了');
			this.$refs.loginBtn.click() //调用登录按钮的登录事件
		},
		keyDown(ev) {
			// console.log('你按方向下键了');
			this.$refs.inputPsw.focus() //输入密码框获取焦点
		},
	},
	created() {
		// this.keyLogin()
	},
}
</script>
